package com.inhim.pjhim.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.TextView;

import com.inhim.pjhim.R;
import com.inhim.pjhim.activity.RadioActivity;
import com.inhim.pjhim.myinterface.ChooseDateInterface;
import com.inhim.pjhim.view.BottomDialog;
import com.inhim.pjhim.view.CenterDialog;
import com.inhim.pjhim.view.TextColorNumberPicker;

import java.lang.reflect.Field;

public class ChooseDateUtil  implements View.OnClickListener, NumberPicker.OnValueChangeListener {

    Context context;
    BottomDialog dialog;
    ChooseDateInterface dateInterface;
    TextColorNumberPicker npYear;
    TextView tvSure;
    private TextView tv_date_title;
    int[] newDateArray = new int[1];

    public void createDialog(Context context, String[] oldDateArray,String title, ChooseDateInterface dateInterface) {
        this.context = context;
        this.dateInterface = dateInterface;
        newDateArray[0] = Integer.parseInt(oldDateArray[0]);
        View outerView = LayoutInflater.from(context).inflate(R.layout.dialog_choose_date, null);
        dialog = new BottomDialog(context, R.style.ActionSheetDialogBotoomStyle);
        //将布局设置给Dialog
        dialog.setContentView(outerView);
        dialog.show();
        //初始化控件
        tvSure = (TextView) outerView.findViewById(R.id.tvSure);
        tvSure.setOnClickListener(this);
        tv_date_title=outerView.findViewById(R.id.tv_date_title);
        tv_date_title.setText(title);
        npYear =  outerView.findViewById(R.id.npYear);
        //设置选择器最小值、最大值
        npYear.setMinValue(1900);
        npYear.setMaxValue(2100);
        //设置选择器初始值
        npYear.setValue(newDateArray[0]);
        //设置监听
        npYear.setOnValueChangedListener(this);
        //去除分割线
        //setNumberPickerDividerColor(npYear);
        //设置字体颜色
        //setNumberPickerTextColor(npYear, Color.parseColor("#1BC47A"));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvSure:
                dialog.dismiss();
                dateInterface.sure(newDateArray);
                break;
        }
    }

    //选择器选择值监听
    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        switch (picker.getId()) {
            case R.id.npYear:
                newDateArray[0] = newVal;
                break;
        }
    }

    private void setNumberPickerDividerColor(TextColorNumberPicker numberPicker) {
        TextColorNumberPicker picker = numberPicker;
        Field[] pickerFields = NumberPicker.class.getDeclaredFields();
        for (Field pf : pickerFields) {
            if (pf.getName().equals("mSelectionDivider")) {
                pf.setAccessible(true);
                try {
                    //设置分割线的颜色值
                    pf.set(picker, new ColorDrawable(context.getResources().getColor(R.color.color_FFC845)));// pf.set(picker, new Div)
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (Resources.NotFoundException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }

    public static boolean setNumberPickerTextColor(NumberPicker numberPicker, int color) {
        boolean result = false;
        final int count = numberPicker.getChildCount();
        for (int i = 0; i < count; i++) {
            View child = numberPicker.getChildAt(i);
            if (child instanceof EditText) {
                try {
                    Field selectorWheelPaintField = numberPicker.getClass()
                            .getDeclaredField("mSelectorWheelPaint");
                    selectorWheelPaintField.setAccessible(true);
                    ((Paint) selectorWheelPaintField.get(numberPicker)).setColor(color);
                    ((EditText) child).setTextColor(color);
                    numberPicker.invalidate();
                    result = true;
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

}
